package com.labichaoka.chaoka.ui.home.fragment.my.withdraw;

import com.labichaoka.chaoka.base.MyApplication;
import com.labichaoka.chaoka.entity.GetWithdrawInfo;
import com.labichaoka.chaoka.entity.GoWithdrawRequest;
import com.labichaoka.chaoka.entity.GoWithdrawResponse;
import com.labichaoka.chaoka.net.BaseSubscriber;
import com.labichaoka.chaoka.net.DataManager;

import rx.Observer;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;
import rx.subscriptions.CompositeSubscription;

public class WithdrawInteractorImpl implements WithdrawInteractor {
    private DataManager manager = new DataManager(MyApplication.mContext);
    private CompositeSubscription mCompositeSubscription = new CompositeSubscription();

    @Override
    public void getWithdrawInfo(OnFinishedListener listener) {
        mCompositeSubscription.add(manager.getWithdrawInfo()
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseSubscriber<GetWithdrawInfo>() {

                    @Override
                    public void onError(Throwable e) {
                        super.onError(e);
                        listener.onGetWithdrawInfoFailed();
                    }

                    @Override
                    public void onNext(GetWithdrawInfo response) {
                        super.onNext(response);
                        listener.onGetWithdrawInfoSucc(response);
                    }
                })
        );
    }

    @Override
    public void goWithdraw(GoWithdrawRequest request, OnFinishedListener listener) {
        mCompositeSubscription.add(manager.goWithdraw(request)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseSubscriber<GoWithdrawResponse>() {

                    @Override
                    public void onError(Throwable e) {
                        super.onError(e);
                        listener.onGoWithdrawFailed();
                    }

                    @Override
                    public void onNext(GoWithdrawResponse response) {
                        super.onNext(response);
                        listener.onGoWithdrawSucc(response);

                    }
                })
        );
    }
}
